package com.ds.infrastructure.hyperspace.console.service.impl;

import com.ds.infrastructure.hyperspace.console.dao.dao.TbContainerActiveConfigDao;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbPublicEnum;
import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbContainerActiveConfig;
import com.ds.infrastructure.hyperspace.console.entity.where.TbContainerActiveConfigCustomWhere;
import com.ds.infrastructure.hyperspace.console.entity.where.TbCustomWhere;
import com.ds.infrastructure.hyperspace.console.service.inf.IContainerActiveConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-09-19 13:44
 **/

@Service
public class ContainerActiveConfigService implements IContainerActiveConfigService {
    @Autowired
    TbContainerActiveConfigDao tbContainerActiveConfigDao;


    @Override
    public int save(TbContainerActiveConfig record) {
        return tbContainerActiveConfigDao.insertSelective(record);
    }

    @Override
    public int update(TbContainerActiveConfig record) {
        return tbContainerActiveConfigDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public TbContainerActiveConfig get(Long id) {
        TbContainerActiveConfig entity = tbContainerActiveConfigDao.selectByPrimaryKey(id);
        if (entity != null && TbPublicEnum.GMD_IS_DEL.NO.code.equals(entity.getGmtIsDeleted())) {
            return entity;
        }
        return null;
    }

    @Override
    public int delete(Long id) {
        TbContainerActiveConfig item = new TbContainerActiveConfig();
        item.setId(id);
        item.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.YES.code);
        return update(item);
    }

    @Override
    public List<TbContainerActiveConfig> getList(TbContainerActiveConfig where, TbCustomWhere customWhere, TbPageWhere pageWhere) {
        return tbContainerActiveConfigDao.selectListForPage(where, customWhere, pageWhere);
    }

    @Override
    public int getTotal(TbContainerActiveConfig where, TbCustomWhere customWhere) {
        return tbContainerActiveConfigDao.selectListForPageCount(where, customWhere);
    }

    @Override
    public TbContainerActiveConfig selectOne(TbContainerActiveConfig where, TbCustomWhere customWhere) {
        List<TbContainerActiveConfig> itemList = tbContainerActiveConfigDao.selectListForPage(where, customWhere, TbPageWhere.newPage4OnlyOne());
        return itemList.stream().findFirst().orElseGet(() -> null);
    }

    @Override
    public TbContainerActiveConfig selectLastOneByStageId(Long stageId) {
        TbContainerActiveConfig where = new TbContainerActiveConfig();
        where.setStageId(stageId);
        where.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.NO.code);
        TbContainerActiveConfigCustomWhere customWhere = new TbContainerActiveConfigCustomWhere().setOrderByType(TbPublicEnum.ORDER_BY_TYPE.ID_DESC.value);
        return selectOne(where, customWhere);
    }
}
